package handels

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"strong-king-mgmt/cache"
	"strong-king-mgmt/models"
	"time"
)

func PendAdd(c *gin.Context) {
	pend_name := c.PostForm("pend_name")
	pend_node := c.PostForm("pend_node")
	pend_from := c.PostForm("pend_from")
	if pend_name == "" || pend_node == "" || pend_from == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.Pend{
		PendName: pend_name,
		PendNode: pend_node,
		PendFrom: pend_from,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加成功",
		})
		return
	}
}
func PendOrderAdd(c *gin.Context) {
	order_to := c.PostForm("order_to")
	order_node := c.PostForm("order_node")
	order_price := c.PostForm("order_price")
	order_good := c.PostForm("order_good")
	if order_to == "" || order_node == "" || order_price == "" || order_good == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.PendOrder{
		OrderTo:    order_to,
		OrderNode:  order_node,
		OrderPrice: order_price,
		OrderGood:  order_good,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加成功",
		})
		return
	}
}
func PendOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var list []models.PendOrderList

	redis_key := "list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("pend_orders").
			Select(
				"pend_orders.id AS OrderID",
				"pends.pend_name AS PendName",
				"pends.pend_node AS PendNode",
				"pends.pend_from AS PendFrom",
				"pend_orders.order_to AS OrderTo",
				"pend_orders.order_node AS OrderNode",
				"pend_orders.order_price AS OrderPrice",
				"pend_orders.order_good AS OrderGood",
			).
			Joins("  LEFT JOIN pends ON pends.id = pend_orders.pid").
			Offset(offset).
			Limit(size).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var count int64
	models.DB.Model(&models.PendOrder{}).Count(&count)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"count": count,
	})
	return
}
func PendOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var list models.PendOrderList

	redis_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Table("pend_orders").
			Select(
				"pend_orders.id as OrderID",
				"pends.pend_name as PendName",
				"pends.pend_node as OrderNode",
				"pends.pend_from as PendFrom",
				"pend_orders.order_to as OrderTo",
				"pend_orders.order_node as OrderNode",
				"pend_orders.order_price as OrderPrice",
				"pend_orders.order_good as OrderGood",
			).
			Joins(" LEFT JOIN pends ON pends.id = pend_orders.pid").
			Where("pend_orders.id = ?", id).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
